package com.aidex.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.aidex.biz.domain.*;
import com.aidex.biz.domain.enums.CustomerEnum;
import com.aidex.biz.domain.enums.DongTaiTypeEnum;
import com.aidex.biz.messageSend.MessageSendService;
import com.aidex.biz.service.*;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.mapper.ToHomeMapper;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 预约上门Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-11-23
 */
@Service
@Transactional(readOnly = true)
public class ToHomeServiceImpl extends BaseServiceImpl<ToHomeMapper, ToHome> implements ToHomeService {
    private static final Logger log = LoggerFactory.getLogger(ToHomeServiceImpl.class);
    @Autowired
    private ToHomeService toHomeService;
    @Autowired
    private CustomerDongtaiService customerDongtaiService;
    @Autowired
    private CustomerShadowInfoService customerShadowInfoService;
    @Autowired
    private ToHomeUserService toHomeUserService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private MessageSendService messageSendService;

    /**
     * 获取单条数据
     *
     * @param toHome 预约上门
     * @return 预约上门
     */
    @Override
    public ToHome get(ToHome toHome) {
        ToHome dto = super.get(toHome);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 预约上门id
     * @return 预约上门
     */
    @Override
    public ToHome get(String id) {
        ToHome dto = super.get(id);
        return dto;
    }


    /**
     * 查询预约上门列表
     *
     * @param toHome 预约上门
     * @return 预约上门
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<ToHome> findList(ToHome toHome) {
        return super.findList(toHome);
    }

    /**
     * 分页查询预约上门列表
     *
     * @param toHome 预约上门
     * @return 预约上门
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<ToHome> findPage(ToHome toHome) {
        return super.findPage(toHome);
    }

    /**
     * 保存预约上门
     *
     * @param toHome
     * @return 结果
     */
    @Override
    public boolean save(ToHome toHome) {
        return super.save(toHome);
    }


    /**
     * 删除预约上门信息
     *
     * @param toHome
     * @return 结果
     */
    @Override
    public boolean remove(ToHome toHome) {
        return super.remove(toHome);
    }

    /**
     * 批量删除预约上门
     *
     * @param ids 需要删除的预约上门ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteToHomeByIds(String[] ids) {
        return mapper.deleteToHomeByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param toHome 预约上门
     * @return 结果
     */
    @Override
    public int findMaxSort(ToHome toHome) {
        return NumberUtils.nextOrder(mapper.findMaxSort(toHome));
    }

    @Override
    public ToHome findOneNearToHome(Integer beforeTime) {
        return mapper.findOneNearToHome(beforeTime);
    }


    /**
     * 上门签到
     */
    @Transactional(readOnly = false)
    @Override
    public boolean Qiandao(ToHome toHome) {
        toHome.setStatus("1");
        toHome.setToHomeTime(new Date());

        // 添加签到人员 默认就是当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ToHomeUser toHomeUser = new ToHomeUser();
        toHomeUser.setTohomeId(toHome.getId());
        toHomeUser.setId(UUID.randomUUID().toString());
        toHomeUser.setIsNewRecord();
        toHomeUser.setDName(user.getDeptName());
        toHomeUser.setUName(user.getName());
        toHomeUser.setUserId(user.getUserId());
        toHomeUser.setUserName(user.getUserName());
        if (null != user.getDeptId()) {
            SysDept sysDept = DeptUtils.getSysDept(user.getDeptId());
            toHomeUser.setDeptId(user.getDeptId());
            toHomeUser.setDName(null == sysDept ? null : sysDept.getDeptName());
            toHomeUser.setDeptName(toHomeUser.getDName());
        }
        toHomeUser.setStatus(CustomerEnum.CUS_TO_HM.getStatus());
        toHomeUser.setYushenStatus("0"); // 待预审
        toHomeUser.setCustomerId(toHome.getCustomerId());
        toHomeUserService.save(toHomeUser);

        CustomerDongtai customerDongtai = new CustomerDongtai();
        customerDongtai.setCustomerId(toHome.getCustomerId());
        customerDongtai.setType(DongTaiTypeEnum.JINJIAN.getStatus());
        customerDongtai.setContent(SecurityUtils.getLoginUser().getUser().getName() + "-上门签到");
        customerDongtaiService.save(customerDongtai);
        // 添加跟进记录
        CustomerShadowInfo customerShadowInfo = new CustomerShadowInfo();
        customerShadowInfo.setTakes(toHome.getRemark());
        customerShadowInfoService.save(customerShadowInfo);

        String cusName = "";
        if (!StringUtils.isEmpty(toHome.getCustomerId())) {
            Customer customer = customerService.get(toHome.getCustomerId());
            customer.setEndTime(new Date());
            customer.setGenjin(customerShadowInfo.getTakes());
            customer.setBusinessStatus(CustomerEnum.CUS_TO_HM.getStatus());
            customerService.save(customer);
            cusName = customer.getCusName();
        }
        // 签到@所有人
        String sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        messageSendService.sendJinjianVxNewsToCompany(null, "签到", new String[]{cusName, toHome.getMeetingRoom(), sdf});
        return this.save(toHome);
    }


    /**
     * 上门反馈
     */
    @Transactional(readOnly = false)
    @Override
    public boolean Fankui(ToHome toHome) {
        CustomerDongtai customerDongtai = new CustomerDongtai();
        customerDongtai.setCustomerId(toHome.getCustomerId());
        customerDongtai.setType(DongTaiTypeEnum.JINJIAN.getStatus());
        customerDongtai.setContent(SecurityUtils.getLoginUser().getUser().getName() + "-上门反馈");
        customerDongtaiService.save(customerDongtai);
        return toHomeService.save(toHome);
    }

}
